/**
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.api.adwords.starterkit.wrappers;

import com.google.api.adwords.starterkit.db.keys.TextAdKey;
import com.google.api.adwords.starterkit.db.structures.TextAdStructure;
import com.google.api.adwords.starterkit.db.tables.TextAdTable;
import com.google.api.adwords.starterkit.settings.TextAdSettings;
import com.google.api.adwords.v201101.cm.AdGroupAdStatus;

import java.sql.Connection;

/**
 * The Class TextAdWrapper manages all TextAd synchronization process with the
 * starterkit DB
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class TextAdWrapper {
  private TextAdStructure txtStructure;

  private TextAdSettings textAdSettings;
  private TextAdTable textAdTable;

  /**
   * Instantiates a new text ad wrapper.
   * 
   * @param adgroupWrapper the adgroup wrapper
   * @param headline the headline
   * @param description1 the description1
   * @param description2 the description2
   * @param destinationUrl the destination url
   * @param textAdSettings the text ad settings
   * @param conn the conn
   */
  public TextAdWrapper(final AdGroupWrapper adgroupWrapper,
      final String headline, final String description1,
      final String description2, final String destinationUrl,
      final TextAdSettings textAdSettings, Connection conn) {
    this.textAdSettings = textAdSettings;
    txtStructure = new TextAdStructure();

    txtStructure.setHeadline(headline);
    txtStructure.setDescription1(description1);
    txtStructure.setDescription2(description2);
    txtStructure.setDestinationUrl(destinationUrl);
    txtStructure.setDisplayUrl(textAdSettings.getDisplayUrl());
    txtStructure.setStatus(AdGroupAdStatus.ENABLED);

    txtStructure.setAdGroupId(adgroupWrapper.getAdGroupId());
    txtStructure.localAdGroupId = adgroupWrapper.getLocalId();

    txtStructure.setStatusModifiable(true);

    textAdTable = new TextAdTable(conn);

    TextAdKey textAdKey =
        new TextAdKey(txtStructure.localAdGroupId, txtStructure.getHeadline(),
            txtStructure.getDescription1(), txtStructure.getDescription2());

    if (textAdTable.exists(textAdKey)) {
      syncronize(textAdKey);
    } else {
      txtStructure = textAdTable.save(txtStructure, true, true);
    }
  }

  public int getLocalId() {
    return txtStructure.getLocalId();
  }

  public String getHeadline() {
    return txtStructure.getHeadline();
  }

  public String getDesc1() {
    return txtStructure.getDescription1();
  }

  public String getDesc2() {
    return txtStructure.getDescription2();
  }

  public int getLocalAdGroupId() {
    return txtStructure.localAdGroupId;
  }

  public long getAdGroupId() {
    return txtStructure.getAdGroupId();
  }

  public String getDestUrl() {
    return txtStructure.getDestinationUrl();
  }

  public String getDispUrl() {
    return txtStructure.getDisplayUrl();
  }

  public AdGroupAdStatus getStatus() {
    return txtStructure.getStatus();
  }

  public void setLocalId(int localId) {
    txtStructure.setLocalId(localId);
    textAdTable.save(txtStructure, true, true);
  }

  public void setTextAdId(long textAdId) {
    txtStructure.setCreativeId(textAdId);
    textAdTable.save(txtStructure, true, true);
  }

  public void setLocalAdGroupId(int localAdGroupId) {
    txtStructure.localAdGroupId = localAdGroupId;
    textAdTable.save(txtStructure, true, true);
  }

  public void setAdGroupId(long adGroupId) {
    txtStructure.setAdGroupId(adGroupId);
    textAdTable.save(txtStructure, true, true);
  }

  public void setDestUrl(String destinationUrl) {
    txtStructure.setDestinationUrl(destinationUrl);
    textAdTable.save(txtStructure, true, true);
  }

  public void setStatus(AdGroupAdStatus status) {
    txtStructure.setStatus(status);
    textAdTable.save(txtStructure, true, true);
  }

  public void setHeadline(String headline) {
    txtStructure.setHeadline(headline);
  }

  public void setDesc1(String desc1) {
    txtStructure.setDescription1(desc1);
  }

  public void setDesc2(String desc2) {
    txtStructure.setDescription1(desc2);
  }

  public boolean getCanModifyStatus() {
    return txtStructure.getCanModifyStatus();
  }

  /**
   * Sets the can modify Status flag. If set to false, StarterKit will not
   * modify the TextAd Status even when direct requested by application.
   * See the canModifyXXXFlags at the manual for a deep dive explanation. 
   */
  public void setCanModifyStatus(boolean canModifyStatus) {
    txtStructure.setStatusModifiable(canModifyStatus);
    textAdTable.save(txtStructure, false, false);
  }

  /**
   * Syncronize DB with application.
   * 
   * @param textAdKey the TextAd primary key
   */
  public void syncronize(TextAdKey textAdKey) {
    TextAdStructure oldkwd = textAdTable.get(textAdKey);

    txtStructure.setAdGroupId(oldkwd.getAdGroupId());
    txtStructure.setCreativeId(oldkwd.getCreativeId());
    txtStructure.localAdGroupId = oldkwd.localAdGroupId;
    txtStructure.setLocalId(oldkwd.getLocalId());

    txtStructure.setStatusModifiable(oldkwd.getCanModifyStatus());

    boolean modified = false;

    if ((textAdSettings.getSyncStatus())
        && (!txtStructure.getStatus().equals(oldkwd.getStatus()))) {
      if (!oldkwd.getCanModifyStatus()) {
        txtStructure.setStatus(oldkwd.getStatus());
      } else {
        modified = true;
      }
    }

    if (modified) {
      textAdTable.save(txtStructure, true, true);
    }
    textAdTable.setTouched(textAdKey, true);
  }
}